<?php 
if (! defined('BASEPATH')) {
    exit('Access Denied');
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>后台管理---导航菜单管理</title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url() ;?><?php echo APPPATH ;?>/views/<?php echo TEMPLETE_STYLE ;?>/easyui.css">
	<link rel="stylesheet" type="text/css" href="<?php echo base_url() ;?><?php echo APPPATH ;?>/views/<?php echo TEMPLETE_STYLE ;?>/admin.css">
	<link rel="stylesheet" type="text/css" href="<?php echo base_url() ;?><?php echo APPPATH ;?>/views/icon.css">
	<script type="text/javascript" src="<?php echo base_url() ;?><?php echo APPPATH ;?>/views/jquery-1.8.0.min.js"></script>
	<script type="text/javascript" src="<?php echo base_url() ;?><?php echo APPPATH ;?>/views/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="<?php echo base_url() ;?><?php echo APPPATH ;?>/views/common.js"></script>
	<script>
	//参考 http://blog.csdn.net/yanghongchang_/article/details/7830111
	$(function(){
		
		$('#tg').treegrid({  
		    url:'<?php echo site_url('admin/nav_manager/index/show_nav');?>?action=ajax',  
		    idField:'id',  
		    treeField:'name',  
		    iconCls: 'icon-ok',
		    rownumbers: false,
		    fitColumns: true,
		    method: 'post',
		    showFooter: true,
		    loadMsg:'正在加载远程数据请稍候。。。。。。',
		    pagination:false, 
			singleSelect:true,
			animate:true, //定义当节点展开/关闭的时候,是否显示动画效果.
			onBeforeLoad:function(){
				//加载之前进行调用
			},
			onLoadSuccess:function(row,data){
				
			//	alert(typeof data.errmsg) ;
				if(typeof data.errmsg == "undefined" || typeof data.resultcode == "undefined" ){
					msgShow("系统警告","服务器返回数据格式出错了","error");
					return ;
				}else if(data.resultcode <0){
					msgShow("系统警告","你没有权限执行此操作请联系管理员","error");
					return ;
				}
			},
			onLoadError:function(){
				msgShow("系统警告","服务器返回数据出错了","error");
				return false ;
			},
			queryParams:"",//查询参数
			onBeforeExpand:function(row){  //菜单展开触发的事件
					
			},
		    columns:[[  
		        {title:'导航ID',field:'id',width:180,checkbox:true},  
		        {field:'name',title:'导航名称',width:180,align:'left'},  
		        {field:'status',title:'状态',width:80},  
		        {field:'url',title:'控制器方法地址',width:180} , 
		        {field:'disorder',title:'排序',width:40} , 
		        {field:'addtime',title:'添加日期',width:120} ,
		        {field:'action',title:'action',width:110} ,
		    ]] 
		}); 
		
	});
//刷新	
function reflash(){
	window.location.href="<?php echo site_url('admin/nav_manager/index/show_nav');?>";
	
}
//生成导航缓存
function make_nav_cache(){
	$.messager.confirm('Waring','是否确定生成导航缓存', function(r){
		if (r){
			progress('loadding.....','<font color="red">正在更新导航缓存,请不要刷新页面.....</font>');
			$.ajax({
				   type: "POST",
				   url: "<?php echo site_url('admin/nav_manager/index/make_nav_cache') ;?>",
				   data: "inajax=1",
				   cache:false,
				   dataType:"json",
				 //  async:false,
				   success: function(msg){
					//console.dir(msg);
					var code = msg.resultcode ;
					if(code <0){
						close_progress();
						msgShow("系统警告",msg.errmsg,"error");
						return false ;
					}else if(code == 0 ){
						close_progress();
						msgShow("系统警告",msg.errmsg,"error");
						return ;
					}else{
						//服务器返回正确的数据
						//msgShow("系统返回信息",msg.errmsg,"alert");
						close_progress();
						msgShow("系统返回信息",msg.errmsg,"alert");
						
					}
					
				   },		   
				   error:function(){
					   alert('服务器返回数据错误....');
				   }
				  
				});	
		}
	});
	
	
	
}
</script>
</head>
<body>
	<h2>后台导航列表菜单</h2>
	<div class="demo-info">
		<div class="demo-tip icon-tip"></div>
		
		<div>注意：第三级是功能接点不会在菜单中显示的,如果是一级菜单请不要填写url地址</div>
	</div>
	<div style="margin:10px 0;">
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-mini-edit'" onclick="edit()" title="编辑数据"> </a>
		<a class="easyui-linkbutton l-btn" data-options="iconCls:'icon-add'" href="javascript:void(0)"  onclick="add_()" title="添加数据" alt="添加数据"></a>
		<a  class="easyui-linkbutton" data-options="iconCls:'icon-remove'" href="javascript:void(0)"  onclick="remove_()" title="删除数据" alt="删除数据"></a>
		<a  class="easyui-linkbutton" data-options="iconCls:'icon-reload'" href="javascript:void(0)"  onclick="reflash()" title="重新加载数据" alt="重新加载数据"></a>
		<a  class="easyui-linkbutton"  href="javascript:void(0)"  onclick="make_nav_cache()" title="生成导航缓存" alt="生成导航缓存">生成导航缓存</a>
	</div>
	<table id="tg" class="easyui-treegrid" title="后台导航列表菜单" style="width:auto;height:auto">
			<!-- 
		<thead>
			<tr>
			<th  data-options="field:'id',checkbox:true"> 导航ID</th>
				<th data-options="field:'name',width:180,editor:'text'">导航名称</th>
				
				<th data-options="field:'status',width:80,editor:'numberbox'">状态</th>	
				<th data-options="field:'url',width:180,editor:'text'">url地址</th>		
				<th data-options="field:'disorder',width:80,editor:'numberbox',align:'center',sortable:false">排序</th>		
				<th data-options="field:'addtime',width:120">添加日期</th>
				
				
				<th data-options="field:'end',width:80,editor:'datebox'">End Date</th>
				<th data-options="field:'progress',width:120,formatter:formatProgress,editor:'numberbox'">Progress</th>
				
			</tr>
		</thead>
		-->
	</table>
<script type="text/javascript">
var editingId;
function remove_(){	
	var tt = $('#tg').datagrid('getSelected');		
	
	if(!tt){
		msgShow("系统警告","请选择进行删除","error");
		return false ;
	}else{
		$.messager.confirm('是否删除'+tt.name, '是否确定此操作', function(r){
				if (r){
						$.ajax({
								   type: "POST",
								   url: "<?php echo site_url('admin/nav_manager/index/deletenav');?>" ,
								   data: "id="+tt.id+"&inajax=1",
								   cache:false,
								   dataType:"json",
								 //  async:false,
								   success: function(msg){
									
									var code = msg.resultcode ;
									if(code <0){
										window.location.href="<?php echo site_url('admin/login/jump_permition_html');?>" ;
										return false ;
									}else if(code == 0 ){
										msgShow("系统警告",msg.errmsg,"error");
										return ;
									}else{
										$('#tg').treegrid();
									}		
								   },
								   
								   error:function(){
									   alert('服务器繁忙,请稍候....');
								   }
								  
						});
				}
		});
	}
			
}
	
function add_(){
	  $('#w').window({
					title: '添加导航',
					width: 620,
					modal: true,
					shadow: true,
					closed: true,
					height: 320,
					resizable:false
	 });	
	 $('#w').window('open');
	 var tt = $('#tg').datagrid('getSelected');		
	 if(tt){
			$("#pid").html("<option value="+tt.id+">"+tt.name+"</option>");		
	 }		
}
//切换大的分类
function switch_pid(){
	$("#pid").html("<option value='0'>大的分类</option>");
}


/*
	处理添加的数据
*/
function submitForm(){
		
			$('#ff').form('submit', {   
				url:'<?php echo site_url('admin/nav_manager/index/addnav') ;?>?action=dopost', 			
			    onSubmit: function(){ 
				  
					var isValid = $(this).form('validate');    
					return isValid ;
			    },   
			    success:function(data){  
				   
			    	var data = $.parseJSON(data);
			    	
			    	if(data.resultcode == 0){
			    		msgShow("系统警告",data.resultinfo.errmsg,"error");
			    		return '' ;
				    }else if(data.resultcode>0){
						
						$("#w").window("close");//关闭窗口
						$('#tg').treegrid();//新加载表格数据
						$('#ff').form('reset');  //重置表单数据
						
					}else{
						
						msgShow("系统信息",data.resultinfo.errmsg,"error");
						return ;
					}
			      
			    }
			
			    
			}); 				
}	

</script>

<div id="w" class="easyui-window" title="导航添加" collapsible="false" minimizable="false"
        maximizable="false" icon="icon-save" data-options="modal:true,closed:true,iconCls:'icon-add'" style="width: 300px; height: 220px; padding: 5px;
        background: #fafafa;">
        <div class="easyui-layout" fit="true">
            <div region="center" border="false" style="padding: 10px; background: #fff; border: 1px solid #ccc;">
            <form id="ff" method="post" >
            	<input type="hidden" name="inajax" value="1">
                <table cellpadding=3>
                    <tr>
                        <td>所属分类:</td>
                        <td id="mypid">
							<select name="pid" id="pid">
								<option value="0">大的分类</option>
							</select>
							<a href="javascript:void(0)" onclick="switch_pid()" class="easyui-linkbutton">切换大的分类</a>
						</td>
                    </tr>					
                    <tr>
                        <td>导航名称：</td>
                        <td><input class="easyui-validatebox"  id="nav_name" type="text" name="nav_name" data-options="required:true" validType="length[3,16]" missingMessage="导航不可以为空" invalidMessage="导航长度必须在6-16位之间"></input></td>
                    </tr>
                     <tr>
                        <td>操作动作：</td>
                        <td><input   type="text" name="myaction"  validType="length[3,16]" missingMessage="导航不可以为空" invalidMessage="导航长度必须在6-16位之间"></input></td>
                    </tr>  
                    <tr>
                        <td>url地址:</td>
                        <td><input class="easyui-validatebox"  id="url" type="text" name="url" data-options="required:true"  missingMessage="url地址不可以为空" invalidMessage="请输入url地址" style="width:240px"></input>备注:如果是大的分类,url地址请填写一个标志</td>
                    </tr>
                    <tr>
                        <td>状态:</td>
                        <td>
						<select class="easyui-validatebox"  id='status' name="status" data-options="required:true" missingMessage="状态请选择" invalidMessage="状态请选择">
							<option value="">请选择</option>
							<option value="1">开启</option>
							<option value="0">关闭</option>
						</select>
						
                    </tr>	
                    <tr>
                        <td>排序:</td>
                        <td><input class="easyui-numberbox"  id="disorder" type="text" name="disorder" data-options="required:true,value:0,min:0,max:128"  missingMessage="请输入排序为整数" invalidMessage="请输入排序为整数"></input></td>
                    </tr>					
                </table>
                </form>
            </div>
            <div region="south" border="false" style="text-align: right; height: 30px; line-height: 24px; margin-top:4px">
                <a id="btnEp" class="easyui-linkbutton" icon="icon-ok" href="javascript:void(0)" onclick="submitForm()">
             			   确定
				</a> 
				<a id="btnCancel" class="easyui-linkbutton" icon="icon-cancel" href="javascript:void(0)" onclick="javascript:$('#w').window('close');">取消</a>
            </div>
        </div>
    </div>	
	



<script>
//编辑的div 窗口
function edit(){
	var row = $('#tg').treegrid('getSelected');	
	if (!row){
		msgShow("系统警告","请选择进行编辑","error");
		return ;
	}	
	editingId = row.id ;
	var loadurl = "<?php echo site_url('admin/nav_manager/index/editnav') ;?>";
	loadurl=loadurl+"?id="+row.id; //加载地址
	$.ajax({
		   type: "POST",
		   url: loadurl,
		   data: "",
		   cache:false,
		   dataType:"json",
		 //  async:false,
		   success: function(msg){
			//console.dir(msg);
			var code = msg.resultcode ;
			if(code <0){
				msgShow("系统警告",msg.errmsg,"error");
				return false ;
			}else if(code == 0 ){
				msgShow("系统警告",msg.errmsg,"error");
				return ;
			}else{
				//服务器返回正确的数据
				//msgShow("系统返回信息",msg.errmsg,"alert");
				
				$('#edit_nav').window('open');  // open a window
				//赋值
				$("#name_edit").val(msg.name);
				
				$("#action_edit").val(msg.action);
				$("#url_edit").val(msg.url);
				
				$("#disorder_edit").val(msg.disorder);
			    $("#status_edit").find("option[value=\""+msg.status+"\"]").attr("selected",true);  
			    
			}
			
		   },		   
		   error:function(){
			   alert('服务器返回数据错误....');
		   }
		  
		});	

	

}
//开始处理编辑
function Submit_edit(){
		var loadurl = "<?php echo site_url('admin/nav_manager/index/editnav') ;?>";
		loadurl=loadurl+"?id="+editingId; //加载地址	
		$('#ff_edit').form('submit', {   
			url:loadurl, 			
		    onSubmit: function(){   			   
				var isValid = $(this).form('validate');    
				return isValid ;
		    },   
		    success:function(data){  
		    	var data = $.parseJSON(data);		    	
		    	if(data.resultcode == 0){
		    		msgShow("系统警告",data.errmsg,"error");
		    		return '' ;
			    }else if(data.resultcode>0){
					//成功
					$('#edit_nav').window('close');//关闭
			    	$('#tg').treegrid();
				}else{
					//小于0 的时候 没权限
					msgShow("系统信息",data.errmsg,"error");
					return ;
				}
		      
		    }
		    
		}); 
}
</script>
<!--编辑页面-->	
<div id="edit_nav" class="easyui-window" title="编辑导航" data-options="modal:true,closed:true,iconCls:'icon-edit'" style="width:660px;height:270px;padding:10px;">
<div region="center" border="false" style="padding: 10px; background: #fff; border: 1px solid #ccc;">
            <form id="ff_edit" method="post" >
            	<input type="hidden" name="inajax" value="1"><!-- 主要是为了判断是ajax提交的 -->
            	<input type="hidden" name="action" value="do">
                <table cellpadding=3>
                 			
                    <tr>
                        <td>导航名称：</td>
                        <td><input class="easyui-validatebox" value="" id="name_edit" type="text" name="name" data-options="required:true" validType="length[3,16]" missingMessage="导航不可以为空" invalidMessage="导航长度必须在6-16位之间"></input></td>
                    </tr>
                  			
                    <tr>
                        <td>操作动作：</td>
                        <td><input  value="" id="action_edit" type="text" name="myaction" style="width:200px"></input></td>
                    </tr>                   
                    <tr>
                        <td>url地址:</td>
                        <td><input class="easyui-validatebox"  id="url_edit" style="width:240px" type="text" name="url" data-options="required:true"  missingMessage="url地址不可以为空" invalidMessage="请输入url地址"></input>备注:如果是大的分类,url地址请填写一个标志</td>
                    </tr>
                    <tr>
                        <td>状态:</td>
                        <td>
						<select class="easyui-validatebox"  id='status_edit' name="status" data-options="required:true" missingMessage="状态请选择" invalidMessage="状态请选择">
							<option value="">请选择</option>
							<option value="1">开启</option>
							<option value="0">关闭</option>
						</select>
						
                    </tr>	
                    <tr>
                        <td>排序:</td>
                        <td><input   id="disorder_edit" type="text" name="disorder"  missingMessage="请输入排序为整数" invalidMessage="请输入排序为整数"></input></td>
                    </tr>					
                </table>
                </form>
            </div>
            <div region="south" border="false" style="text-align: right; height: 30px; line-height: 24px; margin-top:4px">
                <a id="btnEp" class="easyui-linkbutton" icon="icon-ok" href="javascript:void(0)" onclick="Submit_edit()">
             			   确定
				</a> 
				<a id="btnCancel" class="easyui-linkbutton" icon="icon-cancel" href="javascript:void(0)" onclick="$('#edit_nav').window('close')">取消</a>
            </div>
</div>
<!--编辑-->
</body>
</html>